package com.example.dzcfinal;

import android.app.Activity;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.dzcfinal.SQLite.MySQLite;
import com.example.dzcfinal.SQLite.User;

public class EditActivity extends Activity {
    private EditText username_edit;
    private EditText password_edit;
    private EditText sex_edit;
    private EditText age_edit;
    private ImageView backProfile;
    private Button saveBtn;
    private String oldUsername;
    private Integer userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        initHandle();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        username_edit.setText(bundle.getString("username"));
        password_edit.setText(bundle.getString("password"));
        sex_edit.setText(bundle.getString("sex"));
        age_edit.setText(bundle.getString("age"));
        oldUsername = bundle.getString("username");
        userId = bundle.getInt("userId");
        BackProfile();
        SaveEdit();
    }

    private void initHandle(){
        username_edit = findViewById(R.id.username_edit);
        password_edit = findViewById(R.id.password_edit);
        sex_edit = findViewById(R.id.sex_edit);
        age_edit = findViewById(R.id.age_edit);
        backProfile = findViewById(R.id.backProfile);
        saveBtn = findViewById(R.id.saveBtn);

    }

    private void BackProfile(){
        backProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void SaveEdit(){
        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (username_edit.getText().toString().isEmpty()){
                    Toast.makeText(EditActivity.this, "账号还是要有的", Toast.LENGTH_SHORT).show();
                }else if (password_edit.getText().toString().isEmpty()){
                    Toast.makeText(EditActivity.this, "密码都不想设啦", Toast.LENGTH_SHORT).show();
                }else {
                    User user = new User();
                    user.setUsername(username_edit.getText().toString());
                    user.setPassword(new Integer(String.valueOf(password_edit.getText())) );
                    user.setSex(sex_edit.getText().toString());
                    user.setAge(new Integer(String.valueOf(age_edit.getText())));
                    MySQLite sql = new MySQLite(EditActivity.this);
                    sql.editUser(oldUsername,user);
                    Toast.makeText(EditActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(EditActivity.this,ProfileActivity.class);
                    intent.putExtra("userId",userId);
                    startActivity(intent);
                }
            }
        });
    }
}